//
//  MRTurntableDetailViewController.m
//  MoRan
//
//  Created by MoRan on 2024/1/28.
//  Copyright © 2024 MoRan. All rights reserved.
//

#import "MRTurntableDetailViewController.h"

#import "MRTruthDareTitleCell.h"
#import "MRTurntableCell.h"
#import "MRTruthDareButtonCell.h"

#import "MRTruthDareViewController.h"

#import "MRResultAlertView.h"

@interface MRTurntableDetailViewController ()

@property (strong, nonatomic) MRResultAlertView *resultAlertView;

@end

@implementation MRTurntableDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.backgroundColor = [UIColor clearColor];
    [self setNavigationItemTitleViewWithTitle:@"真心话大冒险"];
    [self setRightItemWithIcon:[UIImage imageNamed:@"people_ico"] selector:@selector(gotoTruthDareViewController)];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTruthDareTitleCell" bundle:nil] forCellReuseIdentifier:@"MRTruthDareTitleCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTurntableCell" bundle:nil] forCellReuseIdentifier:@"MRTurntableCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTruthDareButtonCell" bundle:nil] forCellReuseIdentifier:@"MRTruthDareButtonCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
                
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        dict =  @{@"class":MRTruthDareTitleCell.class,
                  @"height":@([MRTruthDareTitleCell getCellFrame:[NSNumber numberWithFloat:84]])};
        [subarr addObject:dict];
        
        dict =  @{@"class":MRTurntableCell.class,
                  @"height":@([MRTurntableCell getCellFrame:[NSNumber numberWithFloat:450]]),
                  @"data":[MRTurntableCellModel ittemModelWithDataList:_dataList],
                  @"delegate":@YES};
        [subarr addObject:dict];
           
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark MRTurntableCellDelegate 完成

- (void)turntableCell:(MRTurntableCell *)cell buttonAction:(UIButton *)sender {
    MRTurntableCell *turntableCell = [self.tableView viewWithTag:10000];
    
    NSInteger count = _dataList.count*2;
    int k = arc4random() % count;
    NSString *str = _dataList[k%_dataList.count];
    
    //创建一个CABasicAnimation来实现自转效果。设置动画的keyPath为"transform.rotation.z"，设置动画的fromValue为0，toValue为M_PI * 2（360度），设置动画的duration为10秒。
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = @(0);
    float toValue = M_PI * (2*5 + (k+0.5)*2/count);
    rotationAnimation.toValue = @(toValue);
    rotationAnimation.duration = 2.0;
    [turntableCell.customView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    //在10秒后执行其他事件，可以使用`dispatch_after`函数来延时执行。
    double delayInSeconds = 2.0;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        // 执行其他事件
        NSLog(@"弹窗 %@",str);
        self.resultAlertView.nameLabel.text = str;
        int index = arc4random() % 2;
        if (index == 0) {
            [self.resultAlertView.typeButton setTitle:@"真心话" forState:UIControlStateNormal];
        } else {
            [self.resultAlertView.typeButton setTitle:@"大冒险" forState:UIControlStateNormal];
        }
        [self.resultAlertView show];
    });
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - goto

- (void)gotoTruthDareViewController {
    MRTruthDareViewController* vc = [MRTruthDareViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (MRResultAlertView *)resultAlertView {
    if (!_resultAlertView) {
        _resultAlertView = [MRResultAlertView instanceView];
        _resultAlertView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _resultAlertView.buttonAction = ^(MRResultAlertView *alertView, UIButton *sender) {
            
        };
        //获取当前UIWindow 并添加一个视图
        UIApplication *ap = [UIApplication sharedApplication];
        [ap.keyWindow addSubview:_resultAlertView];
    }
    return _resultAlertView;
}

@end
